package cda.pager;

import android.graphics.Bitmap;
import android.graphics.Color;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.oned.Code39Writer;
import com.google.zxing.oned.EAN13Writer;
import com.google.zxing.oned.EAN8Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;



public class QRCodeGenerator{
    
    BitMatrix                       _bitMatrix;

    public QRCodeGenerator() {
    
    }
    
    public void generateCode(String type, String data) throws WriterException, FileNotFoundException{
        String text = data; // this is the text that we want to encode  
  
        int width = 800;  
        int height = 300; // change the height and width as per your requirement  

        // (ImageIO.getWriterFormatNames() returns a list of supported formats)  
        String imageFormat = "png"; // could be "gif", "tiff", "jpeg"   

        if(type.equals("EAN_13")){
            _bitMatrix = new EAN13Writer().encode(text, BarcodeFormat.EAN_13, width, height);
        }else if(type.equals("EAN_8")){
            _bitMatrix = new EAN8Writer().encode(text, BarcodeFormat.EAN_8, width, height);
        }else if(type.equals("CODE_128")){
            _bitMatrix = new Code128Writer().encode(text, BarcodeFormat.CODE_128, width, height);
        }else if(type.equals("CODE_39")){
            _bitMatrix = new Code39Writer().encode(text, BarcodeFormat.CODE_39, width, height);
        }else if(type.equals("QR_CODE")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
        }else if(type.equals("DATA_MATRIX")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.DATA_MATRIX, width, height);
        }else if(type.equals("AZTEc")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.AZTEC, width, height);
        }else if(type.equals("ITF")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.ITF, width, height);
        }else if(type.equals("UPC_A")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.UPC_A, width, height);
        }else if(type.equals("UPC_E")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.UPC_E, width, height);
        }else if(type.equals("CODABAR")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.CODABAR, width, height);
        }else if(type.equals("CODE_93")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.CODE_93, width, height);
        }else if(type.equals("PDF_417")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.PDF_417, width, height);
        }else if(type.equals("RSS_14")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.RSS_14, width, height);
        }else if(type.equals("RSS_EXPANDED")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.RSS_EXPANDED, width, height);
        }else if(type.equals("UPC_EAN_EXTENSION")){
            _bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.UPC_EAN_EXTENSION, width, height);
        }

        int wwidth = _bitMatrix.getWidth();
        int hheight = _bitMatrix.getHeight();
        int[] pixels = new int[wwidth * hheight];
        // All are 0, or black, by default
        for (int y = 0; y < hheight; y++) {
            int offset = y * wwidth;
            for (int x = 0; x < wwidth; x++) {
                pixels[offset + x] = _bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        
        try{
            FileOutputStream out = new FileOutputStream("/sdcard/cda.memo/out.png");
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        }catch(Exception e){
            e.printStackTrace();
        }
    }  
}